<?php
use com\gdl\common\dataobject\common\ResultDO; 
use com\gdl\kq\interfaces\AttAdminPositionServiceIf;
use com\gdl\kq\dataobject\adminposition\PositionDTO; 
use com\gdl\kq\dataobject\adminposition\PositionInfoDTO; 
require(dirname(dirname(__FILE__)) ."/php_sdk-master/include.php");
use MagicCrypt\MagicCrypt;
/**
 * 考勤系统Admin后台用户相关接口 
 * 此接口包含后台用户列表 修改 添加 相关接口
 */
class api_AttAdminPositionService extends api_Abstract implements AttAdminPositionServiceIf{
	 
	
	//获取带分组的职位列表 联动模式
	public function getPositionGroup(PositionDTO $position){
		#接受参数
		$data = $position->data ? gdl_lib_BaseUtils::getStr($position->data) : "";
		
		$offset = $user->offset ? gdl_lib_BaseUtils::getStr($user->offset, 'int') : 0;
        $num = $user->num ? gdl_lib_BaseUtils::getStr($user->num, 'int') : 10;
		$positionName = $data['positionName'] ? $data['positionName'] : "";
		$unitsId = $data['unitsId'] ? $data['unitsId'] : "";
		$platformId = $data['platformId'] ? $data['platformId'] : "";
		
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 分页 备用
		/*if($offset=='0') $offset=1;
		$limit = ($offset - 1) * $num . "," . $num;
		$group = 'order by atd_position_group.id desc limit '.$limit;*/		
		# 验证
		
		try{
			$positionGroupModel = new model_attendance_positiongroup();
			$positionModel = new model_attendance_position();
			
			# 查询分组
			$positionGroupWhere = "isdelete =0 ";
			
			# 平台查询条件 备用
			if($platformId){
				$positionGroupWhere.=" and platformId={$platformId} ";
			}
			/*if($unitsId){
				$positionGroupWhere.=" and unitId={$unitsId} ";
			}*/
			
			$positionGroupFiled = "id,title,sort,pid";
			$positionGroupList = $positionGroupModel->select($positionGroupWhere, $positionGroupFiled, 'order by id desc')->items;
			
			# 查询职位
			$positionWhere = "isdelete =0 and platformId={$platformId}";
			$positionFiled = "id,title,sort,groupId as pid";
			$positionList = $positionModel->select($positionWhere, $positionFiled, 'order by id desc')->items;
			 
			# 没有分组 直接返回职位
			if(empty($positionGroupList) && !empty($positionList)){
				$result->message = "获取成功";
				$result->code = 200;
				$resultData['list'] = json_encode($positionList);
				$result->data[]= $resultData;
			}
			
			#职位 分组都没有
			if(empty($positionList) && empty($positionGroupList)){$result->message = "暂无职位"; return $result;}
			
			#最终组合数组
			for($i=0 ; $i<count($positionGroupList); $i++){
				$positionGroupList[$i]['children'] = [];
				
				# 如果职位没有则不需要遍历
				if(!empty($positionList)){
					for($j=0; $j<count($positionList); $j++){
						if($positionGroupList[$i]['id']==$positionList[$j]['pid']){
							$positionGroupList[$i]['children'][] = ['id'=>$positionList[$j]['id'],'title'=>$positionList[$j]['title'],'sort'=>$positionList[$j]['sort'],'pid'=>$positionList[$j]['pid']];
						}
					}
				}
				
			}
			
			# 返回数据
			$result->message = "获取成功";
			$result->code = 200;
			$resultData['list'] = json_encode($positionGroupList);
			$result->data[] = $resultData;
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result; 
	}
	
	//获取存职位列表
	public function getPositionList(PositionDTO $position){
		#接受参数
		$data = $position->data ? gdl_lib_BaseUtils::getStr($position->data) : "";
		
		$offset = $user->offset ? gdl_lib_BaseUtils::getStr($user->offset, 'int') : 0;
        $num = $user->num ? gdl_lib_BaseUtils::getStr($user->num, 'int') : 100000;
		$positionName = $data['positionName'] ? $data['positionName'] : "";
		$unitsId = $data['unitsId'] ? $data['unitsId'] : "";
		$platformId = $data['platformId'] ? $data['platformId'] : "";
		$groupId = $data['groupId'] ? $data['groupId'] : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		
		try{
			$positionModel = new model_attendance_position();
			$where = "isdelete = 0 ";
			if($platformId){$where.=" and platformId={$platformId}";}
			if($positionName){$where.=" and title like '%".$positionName."%' ";}
			if($unitsId){$where.=" and unitId={$unitsId} ";} # 是否要加入单位id
			if($groupId){$where.=" and groupId={$groupId}";} # 按照分组id查询
			$filed = "id, title";
			
			# 数据集
			$positionList = $positionModel->select($where, $filed, 'ORDER BY id DESC')->items;
			if($positionList){
				$result->message = "";
				$result->code = 200;
				$result->data = $positionList;
			}else{
				$result->message = "暂无职位";
			}
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result; 
	}
	
	//添加编辑职位分组 type=add type=edit
	public function editGroup(PositionDTO $position){
		#接受参数
		$data = $position->data ? gdl_lib_BaseUtils::getStr($position->data) : "";
		$type = $position->type ? gdl_lib_BaseUtils::getStr($position->type) : "add";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($data['title'])){$result->message = "分组名不能为空"; return $result;}
		$id = $data['id']?trim($data['id']):"";
		
		# id存在为修改 id不存在为添加
		if(!empty($id)){#修改
			$upData['title'] = $data['title'];
			$upData['updateTime'] = time();
			$upData['sort'] = $data['sort']?$data['sort']:0;
			
			$where = "id ={$data['id']}";
			
		}else{ #添加
			$upData['title'] = $data['title']?$data['title']:0;
			$upData['createTime'] = time();
			$upData['sort'] = $data['sort']?$data['sort']:0;
			$upData['platformId'] = $data['platformId']?$data['platformId']:0;
			$upData['unitId'] = $data['unitId']?$data['unitId']:0;
			$upData['isdelete'] = 0;
		}
		
		try{
			$positionGroupModel = new model_attendance_positiongroup();
			$ret = false;
			if($type=='add'){
				$ret = $positionGroupModel->insert($upData);
				$result->message = "添加成功";
			}
			if($type=='edit'){
				$ret = $positionGroupModel->update($where, $upData);
				$result->message = "修改成功";
			}
			
			if($ret){
				$result->code = 200;
			}else{
				$result->message = "操作异常，请重新登录";
			}
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result; 
	}
	
	//添加编辑职位
	public function editPosition(PositionDTO $position){
		#接受参数
		$data = $position->data ? gdl_lib_BaseUtils::getStr($position->data) : "";
		$type = $position->type ? gdl_lib_BaseUtils::getStr($position->type) : "add";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($data['title'])){$result->message = "职位名名不能为空"; return $result;}
		$id = $data['id']?trim($data['id']):"";
		
		# id存在为修改 id不存在为添加
		if(!empty($id)){#修改
			$upData['title'] = $data['title'];
			$upData['updateTime'] = time();
			//$upData['groupId'] = $data['groupId'];
			$upData['sort'] = $data['sort']?$data['sort']:0;
			
			$where = "id ={$data['id']}";
			
		}else{ #添加
			$upData['title'] = $data['title']?$data['title']:0;
			$upData['groupId'] = $data['groupId']?$data['groupId']:0;
			$upData['createTime'] = time();
			$upData['sort'] = $data['sort']?$data['sort']:0;
			$upData['platformId'] = $data['platformId']?$data['platformId']:0;
			$upData['unitId'] = $data['unitId']?$data['unitId']:0;
			$upData['isdelete'] = 0;
		}
		
		try{
			$positionModel = new model_attendance_position();
			$ret = false;
			if($type=='add'){
				$ret = $positionModel->insert($upData);
				$result->message = "添加成功";
			}
			if($type=='edit'){
				$ret = $positionModel->update($where, $upData);
				$result->message = "修改成功";
			}
			
			if($ret){
				$result->code = 200;
			}else{
				$result->message = "操作异常，请重新登录";
			}
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result; 
	}
     
	//删除分组
	public function deleteGroup(PositionInfoDTO $position){
		# 需要删除的用于 逗号分隔字符串 
		$idstr = $position->idstr ? gdl_lib_BaseUtils::getStr($position->idstr) : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($idstr)){$result->message = "请选择用户"; return $result;}
		
		try{
			$positionGroupModel = new model_attendance_positiongroup();
			
			#分组下还有职位不能删除
			$positionModel = new model_attendance_position();
			$positionWhere = " isdelete=0 and groupId in(".$idstr.")";
			$emptyPosition = $positionModel->selectOne($positionWhere, 'id')->items;
			if(!empty($emptyPosition)){$result->message = "该分组下还有职位，无法删除"; return $result;}
			
			$where = "id in (".$idstr.")";
			$delete['isdelete'] = 1;
			$ret = $positionGroupModel->update($where, $delete);
			if($ret){
				$result->message = "删除成功";
				$result->code = 200;
			}else{
				$result->message = "分组已删除，请刷新页面";
			}
				
		}catch(Exception $e){
			$result->message  = $e->getMessage();
		}
		 
        return $result; 
	} 
	//删除职位
	public function deletePosition(PositionInfoDTO $position){
		# 需要删除的用于 逗号分隔字符串 
		$idstr = $position->idstr ? gdl_lib_BaseUtils::getStr($position->idstr) : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($idstr)){$result->message = "请选择要删除的职位"; return $result;}
		
		try{
			$positionModel = new model_attendance_position();
			
			#职位下有用户不能删除
			$userModel = new model_attendance_member();
			$userWhere = " status=2 and positionId in(".$idstr.")";
			$emptyUesr = $userModel->selectOne($userWhere, 'id')->items;
			if(!empty($emptyUesr)){$result->message = "该职位下还有用户，无法删除"; return $result;}
			
			$where = "id in (".$idstr.")";
			$delete['isdelete'] = 1;
			$ret = $positionModel->update($where, $delete);
			if($ret){
				$result->message = "删除成功";
				$result->code = 200;
			}else{
				$result->message = "职位已删除，请刷新页面";
			}
				
		}catch(Exception $e){
			$result->message  = $e->getMessage();
		}
		 
        return $result; 
	}
	
	// 分组详情
	public function groupInfo(PositionInfoDTO $position){
		$id = $position->id ? gdl_lib_BaseUtils::getStr($position->id) : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($id)){$result->message = "请选择分组"; return $result;}
		
		try{
			$positionGroupModel = new model_attendance_positiongroup();
			$where = "id={$id}";
			$filed = "id,title";
			$info = $positionGroupModel->selectOne($where, $filed);
			if($info){
				$newList['list'] = json_encode($info);
				$result->message = "获取成功";
				$result->code = 200; 
				$result->data[] = $newList;
			}else{
				$result->message = "获取失败";
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		return $result;
	}
	
	// 职位详情
	public function positionInfo(PositionInfoDTO $position){
		$id = $position->id ? gdl_lib_BaseUtils::getStr($position->id) : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($id)){$result->message = "请选择职位"; return $result;}
		
		try{
			#职位
			$positionModel = new model_attendance_position();
			$where = "id={$id}";
			$filed = "id,title,groupId";
			$info = $positionModel->selectOne($where, $filed);
			
			if($info){
				$newList['list'] = json_encode($info);
				$result->message = "获取成功";
				$result->code = 200; 
				$result->data[] = $newList;
			}else{
				$result->message = "获取失败";
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		return $result;
	}
	
	//分组列表
	public function myPositionList(PositionInfoDTO $position){
		#接受参数
		$data = $position->idarr ? gdl_lib_BaseUtils::getStr($position->idarr) : "";
		
		$unitsId = $data['unitsId'] ? $data['unitsId'] : "";
		$platformId = $data['platformId'] ? $data['platformId'] : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = []; 
		
		#验证
		if(empty($platformId)){$result->message = "请选择平台"; return $result;}
		
		try{
			$groupModel = new model_attendance_positiongroup();
			$where = "platformId={$platformId} and isdelete=0";
			$filed = "id,title";
			$info = $groupModel->select($where, $filed)->items;
			if($info){
				$result->data = $info;
				$result->code = 200; 
				$result->message = "获取成功";
			}else{
				$result->message = "获取失败";
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		return $result;
	}
}

